#!/bin/bash


IFS=","
LINE="houxin-register=172.18.1.21=8848:8848,houxin-gateway=172.18.1.22=9999:9999,houxin-auth=172.18.1.23=3000:3000,houxin-admin=172.18.1.24=4000:4000"
ARR=($LINE)
DOCKER_NETWORK=zima-net
# shellcheck disable=SC2068

for dn in ${ARR[@]}
   do
      DOCKER_NAME=`echo ${dn} | awk -F  '=' '{print $1}'`
      DOCKER_IP=`echo ${dn} | awk -F  '=' '{print $2}'`
      DOCKER_PORT=`echo ${dn} | awk -F  '=' '{print $3}'`
      IMAGE_NAME=houxin:5000/${DOCKER_NAME}:v${BUILD_ID}
      DOCKER_ID=`/usr/bin/docker ps -a  | grep "${DOCKER_NAME}" | awk -F ' ' '{print $3}'`

       # 创建目录
      mkdir -p /opt/module/houxin/${DOCKER_NAME}/logs
      # 删除之前的容器和镜像
       if [ -n "${DOCKER_ID}" ]; then
           echo "Stop container: ${DOCKER_ID}"
           /usr/bin/docker stop ${DOCKER_ID}
            echo "Delete exists Container_Id: "${DOCKER_ID}
           /usr/bin/docker rm ${DOCKER_ID}
           /usr/bin/docker  images  | grep "${IMAGE_NAME}"  | awk '{print $3}'  |  xargs docker rmi
        fi
        # 运行容器
       /usr/bin/docker run -e TZ="Asia/Shanghai" --privileged  \
-itd -h ${DOCKER_NAME}.com --name ${DOCKER_NAME} \
-v /opt/module/houxin/${DOCKER_NAME}/logs:/usr/lib/java/logs/${DOCKER_NAME}  \
-p ${DOCKER_PORT} --network ${DOCKER_NETWORK}=  --ip ${DOCKER_IP}  ${IMAGE_NAME}
   done













